package web.beans;

import User.model.User;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.Locale;

/**
 * Created by Новак on 24.11.2014.
 */
@ManagedBean
@SessionScoped
public class UserBean implements Serializable {
    private int id;
    private String login;
    private String name;
    private int accessLevel;
    private String avatar;
    private boolean moderator=false;
    private String locale="ru";

    public UserBean(){
        ;
    }
    public void setUserInfo(User user){
        if(user!=null){
            id=user.getId();
            login=user.getLogin();
            name=user.getFirst_name();
            accessLevel=user.getAccess_level();
            avatar=user.getAvatar();
            if(accessLevel>1){
                moderator=true;
            }
        }
        else{
            id=0;
            login=null;
            name=null;
            accessLevel=0;
        }

    }
    public String close(){
        id=0;
        login=null;
        name=null;
        accessLevel=0;
        moderator=false;
        return "quest";
    }

    private void editLocale(String locale){
        UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
        viewRoot.setLocale(new Locale(locale));
    }
    public void setLocaleEn(){
        locale="en";
        editLocale(locale);
    }
    public void setLocaleRu(){
        locale="ru";
        editLocale(locale);
    }



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAccessLevel() {
        return accessLevel;
    }

    public void setAccessLevel(int accessLevel) {
        this.accessLevel = accessLevel;
    }

    public boolean isModerator() {
        return moderator;
    }

    public void setModerator(boolean moderator) {
        this.moderator = moderator;
    }

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
}
